﻿#include "EditorObjectPropertyAbilityViewModel.h"

#include "Asset/EditorDefaultPackage.h"
#include "Scene/SceneNode.h"

#include "Framework/Editor.h"

#include "Property/EditorPropertyModel.h"
#include "Property/EditorPropertyViewModel.h"

void REditorObjectPropertyAbilityViewModel::BindModel(TSharedObjectPtr<REditorAbilityModel> InModel)
{
    Super::BindModel(InModel);

    PropertyModel =InModel;

    Icon = REditorDefaultPackage::Get()->GetIcon(U"Property");
    HighlightIcon = REditorDefaultPackage::Get()->GetIcon(U"Property_Highlight");

    PropertyModel->OnPropertyListChanged.Bind(
        [this]()
        {
            UpdateProperties();
            
        }
    );
}

TSharedObjectPtr<RTexture2D> REditorObjectPropertyAbilityViewModel::GetIcon() const
{
    return Icon;
}

TSharedObjectPtr<RTexture2D> REditorObjectPropertyAbilityViewModel::GetHighlightIcon() const
{
    return HighlightIcon;
}

bool REditorObjectPropertyAbilityViewModel::HasObject() const
{
    return PropertyModel->GetObject().IsValid();
}

int64_t REditorObjectPropertyAbilityViewModel::GetPropertiesNum() const
{
    return Properties.Num();
}

TSharedObjectPtr<REditorPropertyViewModel> REditorObjectPropertyAbilityViewModel::GetProperty(int64_t InIndex) const
{
    return Properties[InIndex];
}

void REditorObjectPropertyAbilityViewModel::AddComponent(TSubclassOf<RSceneNodeComponent> InComponentClass)
{
    PropertyModel->AddComponent(InComponentClass);
}

void REditorObjectPropertyAbilityViewModel::RemoveComponent(TSharedObjectPtr<RSceneNodeComponent> InComponent)
{
    PropertyModel->RemoveComponent(InComponent);
}

void REditorObjectPropertyAbilityViewModel::UpdateProperties()
{
    Properties.Clear();

    for(int64_t i = 0; i < PropertyModel->GetPropertiesNum(); i++)
    {
        CClass* PropertyViewModelClass=
            REditor::Get()->GetViewModelClass(PropertyModel->GetProperty(i)->GetClass());
        TSharedObjectPtr<REditorPropertyViewModel> PropertyViewModel = NewObject<REditorPropertyViewModel>(PropertyViewModelClass,this);
        PropertyViewModel->BindModel(PropertyModel->GetProperty(i),this);
        Properties.Add(PropertyViewModel);
    }


    OnPropertyListChanged.ExecuteIfBound();
}
